#pragma once
#include<iostream>
#include<pthread.h>
#include<functional>
// template<class T>

class ThreadData
{
    public:
    ThreadData(std::string threadname,pthread_mutex_t*mutex)
    :_threadname(threadname)
    ,_mutex(mutex)
    {}
    public:
    std::string _threadname;
    pthread_mutex_t*_mutex;
};
using func_t = std::function<void(ThreadData*)>;

//template<class T>

class Thread
{
    public:
    Thread(const std::string threadname,func_t func,ThreadData* data)
    :_threadname(threadname)
    ,_tid(0)
    ,_func(func)
    ,_data(data)
    ,_isrunning(false)
    {}
    static void*ThreadRoutine(void*args)
    {
        Thread*td=static_cast<Thread*>(args);
        td->_func(td->_data);
        return nullptr;
    }
    bool Start()
    {
        int n = pthread_create(&_tid,nullptr,ThreadRoutine,this);
        if(n==0)
        {
            _isrunning=true;
            return true;
        }
        else return false;
    }
    bool Join()
    {
        if(!_isrunning) return true;

            int n=pthread_join(_tid,nullptr);
            if(n==0)
            {
                _isrunning=false;
                return true;
            }
            return false;
        
    }
    std::string ThreadName()
    {
        return _threadname;
    }
    bool IsRunning()
    {
        return _isrunning;
    }
    ~Thread()
    {}
    public:
    pthread_t _tid;
    std::string _threadname;
    func_t _func;
    ThreadData* _data;
    bool _isrunning;
};